package com.howie.parnote.util;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 分页工具
 */
public class PageUtil {

	public enum PageSize {
		DEFAULT_SIZE(15), MAX_SIZE(99);

		private int size;

		private PageSize(int size) {
			this.size = size;
		}

		public int getSize() {
			return size;
		}
	}

	public static final String OFFSET = "offset";
	public static final String LIMIT = "limit";

	public static Map<String, Integer> init(Page<?> page, HttpServletRequest request) {
		Map<String, Integer> params = new HashMap<String, Integer>(2);
		int pageNumber = Integer.parseInt(StringUtils.defaultIfBlank(request.getParameter("p"), "1"));
		page.setPageNo(pageNumber);
		// int pageSize =
		// Integer.parseInt(StringUtils.defaultIfBlank(request.getParameter("ps"),
		// String.valueOf(PAGE_SIZE)));
		page.setPageSize(page.getPageSize());
		params.put(OFFSET, page.getFirst() - 1);
		params.put(LIMIT, page.getPageSize());
		return params;
	}

}
